package FourArithmeticOperations;

/**
 * Created by dell on 2017/5/21.
 */
import java.util.Random;
import java.util.Scanner;
import java.text.NumberFormat;
public class Questions {
    public static void main(String[] args) {
        RationalNumber N1, N2 = null, N3 = null;
        String Q, A;
        int questions, ans,trues =0;
        Scanner scan = new Scanner(System.in);
        Random genarator = new Random();
        PostfixEvaluator evaluator = new PostfixEvaluator();
        System.out.println("请输入需要的题目数量：");
        questions = scan.nextInt();
        for (int n = 0; n<questions; n++){
            int na = genarator.nextInt(2);
            int m = n +1;
            switch(na){
                case 0:
                    System.out.println("第" + m + "题");
                    Intquestions a = new Intquestions();
                    System.out.println(a.getIntquestions() + "=");
                    ans = scan.nextInt();

                    String input = a.getIntquestions();
                    String output;
                    InToPost theTrans = new InToPost(input);
                    output = theTrans.doTrans();


                    int result = evaluator.evaluate(output);

                    if (ans == result)
                        trues++;
                    else
                        System.out.println("正确答案： " + result);
                    break;


                case 1:
                    System.out.println("第" + m + "题");
                    int nb = genarator.nextInt(4);
                    N2 = RationalNumber.obj();
                    N3 = RationalNumber.obj();
                    switch (nb) {
                        case 0:
                            N1 = N2.add(N3);
                            Q = N1.toString();
                            System.out.print("(" + N2.toString() + ")" + " + " + "(" + N3.toString() + ")" + "=");
                            A = scan.next();
                            if (A.equals(Q))
                                trues++;
                            else
                                System.out.println("正确答案： " + Q);
                            break;
                        case 1:
                            N1 = N2.subtract(N3);
                            Q = N1.toString();
                            System.out.print("(" + N2.toString() + ")" + " - " + "(" + N3.toString() + ")" + "=");
                            A = scan.next();
                            if (A.equals(Q))
                                trues++;
                            else
                                System.out.println("正确答案： " + Q);
                            break;
                        case 2:
                            N1 = N2.multiply(N3);
                            Q = N1.toString();
                            System.out.print("(" + N2.toString() + ")" + " * " + "(" + N3.toString() + ")" + "=");
                            A = scan.next();
                            if (A.equals(Q))
                                trues++;
                            else
                                System.out.println("正确答案： " + Q);
                            break;
                        case 3:
                            N1 = N2.divide(N3);
                            Q = N1.toString();
                            System.out.print("(" + N2.toString() + ")" + " / " + "(" + N3.toString() + ")" + "=");
                            A = scan.next();
                            if (A.equals(Q))
                                trues++;
                            else
                                System.out.println("正确答案： " + Q);
                            break;
                    }

            }
        }
        System.out.println("答对数： " + trues);
        NumberFormat fmt = NumberFormat.getPercentInstance();
        double rate = (double) trues / questions;
        System.out.println("正确率： " + fmt.format(rate));
    }
}